package com.iflysse.services.impl;

import com.iflysse.common.ResultEnum;
import com.iflysse.dao.CourseDao;
import com.iflysse.dao.TeacherDao;
import com.iflysse.dao.TeachingClassDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.*;
import com.iflysse.pojo.bo.resp.*;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.*;
import com.iflysse.pojo.po.resp.CourseDetailPoResp;
import com.iflysse.pojo.po.resp.TeachingClassNamePoResp;
import com.iflysse.pojo.po.resp.TeachingClassPoResp;
import com.iflysse.pojo.vo.resp.TeacherAllClassResp;
import com.iflysse.services.TeacherClassService;
import com.iflysse.util.CommonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherClassServiceImpl implements TeacherClassService {

    public static final Logger log = Logger.getLogger(TeacherClassServiceImpl.class);
    @Autowired
    TeacherDao teacherDao;
    @Autowired
    TeachingClassDao teachingClassDao;
    @Autowired
    CourseDao courseDao;


    @Override
    public TeacherAllClassBoResp queryAllClassByUserId(TeacherClassBoReq teacherClassBoReq) throws SQLException {

        TeacherAllClassBoResp teacherAllClassBoResp = new TeacherAllClassBoResp();

        //根据userId查询出教师编号

        //参数转换， BO——>PO
        UserIdPoReq userIdPoReq = CommonUtil.objToObj(teacherClassBoReq, UserIdPoReq.class);

        //根据教师编号查询该教师下所有得班级信息
        List<TeachingClassPoResp> list = teachingClassDao.getAllTeachingClassesByTeacherId(userIdPoReq.getAccountId());
        log.info("根据教师编号" + userIdPoReq.getAccountId() + "查询得班级列表信息为：" + list);

        List<TeacherAllClassResp> teacherAllClassResps = new ArrayList<TeacherAllClassResp>();
        list.forEach(teachingClassPoResp -> {
            TeacherAllClassResp teacherAllClassResp = new TeacherAllClassResp();
            //班级编号
            teacherAllClassResp.setClassId(teachingClassPoResp.getTeachingClassID());
            //班级名称
            teacherAllClassResp.setClassName(teachingClassPoResp.getTeachingClassName());
            //班级学生数
            teacherAllClassResp.setClassNum(teachingClassPoResp.getStudentAmount());

            teacherAllClassResps.add(teacherAllClassResp);
        });

        teacherAllClassBoResp.setResult(teacherAllClassResps);
        log.info("根据教师编号" + teacherClassBoReq.getAccountId() + "查询得班级列表信息为：" + teacherAllClassBoResp);
        return teacherAllClassBoResp;
    }

    /**
     * 老师端,获取所有教学班级
     *
     * @return
     */
    @Override
    public TeachingClassNameListBoResp queryAllTeacherClassNames() throws SQLException {
        TeachingClassNameListBoResp teachingClassNameListBoResp = new TeachingClassNameListBoResp();
        //获取系统中所有的教学班级名称
        List<TeachingClassNamePoResp> allTeachingClassName = teachingClassDao.getAllTeachingClassName();
        //将po转bo
        allTeachingClassName.forEach(teachingClassNamePoResp -> {
            teachingClassNameListBoResp
                    .getTeachingClassNameBoRespList()
                    .add(CommonUtil.objToObj(teachingClassNamePoResp, TeachingClassNameBoResp.class));
        });

        return teachingClassNameListBoResp;
    }

    @Override
    public CommonBoResp addTeacherClass(AddTeacherClassBoReq addTeacherClassBoReq) throws SQLException {

        CommonBoResp commonBoResp = new CommonBoResp();
        TeachingClassPoReq teachingClassPoReq = new TeachingClassPoReq();

        //根据教学班级名称查询教学班级
        TeachingClassPoResp teachingClassPoResp = teachingClassDao.getTeachingClassByClassName(addTeacherClassBoReq.getClassName());
        //教学班级不存在
        if (teachingClassPoResp == null) {
            teachingClassPoReq.setCourseId(addTeacherClassBoReq.getCourseId());//课程编号
            teachingClassPoReq.setTeacherId(addTeacherClassBoReq.getUserId());//教师编号
            teachingClassPoReq.setTeachingClassName(addTeacherClassBoReq.getClassName());//班级名称
            int i = teachingClassDao.addTeachingClass(teachingClassPoReq);

            CommonUtil.printToBoResult(commonBoResp, i);


        } else {
            commonBoResp.setMsg("教学班级名称" + ResultEnum.HASEXISTED.getMsg());
            commonBoResp.setCode(ResultEnum.HASEXISTED.getCode());

        }

        return commonBoResp;
    }

    /**
     * 老师端删除班级信息
     *
     * @param deleteTeacherClassBoReq
     * @return
     * @throws SQLException
     */
    @Override
    public CommonBoResp deleteTeacherClass(DeleteTeacherClassBoReq deleteTeacherClassBoReq) throws SQLException {
        CommonBoResp commonBoResp = new CommonBoResp();
        //TODO 判断是否已经存在教学数据

        int i = teachingClassDao.deleteTeachingClassByTeachingClassId(deleteTeacherClassBoReq.getClassId());
        CommonUtil.printToBoResult(commonBoResp, i);
        return commonBoResp;
    }

    /**
     * 学生批量绑定到教学班级
     *
     * @param studentInTeacherClassBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp addStudentToTeacherClass(StudentInTeacherClassBoReq studentInTeacherClassBoReq) throws SQLException {
        CommonBoResp commonBoResp = null;
        StudentInTeachingClassPoReq studentInTeachingClassPoReq
                = CommonUtil.objToObj(studentInTeacherClassBoReq, StudentInTeachingClassPoReq.class);
        studentInTeachingClassPoReq.setTeachingClassId(studentInTeacherClassBoReq.getTeacherClassId());
            CommonPoResp commonPoResp=new CommonPoResp();
            int i = teachingClassDao.addStudentToTeachingClass(studentInTeachingClassPoReq);
            CommonUtil.printToPoResult(commonPoResp,i);
            commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
            updateStudentAmountOfTeachingClass(studentInTeachingClassPoReq);

        return commonBoResp;
    }

    /**
     * 更新教学班级人数
     * @param studentInTeachingClassPoReq
     * @throws SQLException
     */
    private void updateStudentAmountOfTeachingClass(StudentInTeachingClassPoReq studentInTeachingClassPoReq) throws SQLException {
        //获得班级最新人数
        int amount = teachingClassDao
                .getStudentAmountByTeachingClassId(studentInTeachingClassPoReq.getTeachingClassId());

        UpdateAmountOfStudentPoReq updateAmountOfStudentPoReq = new UpdateAmountOfStudentPoReq();
        updateAmountOfStudentPoReq.setStudentAmount(amount);
        updateAmountOfStudentPoReq.setTeachingClassId(studentInTeachingClassPoReq.getTeachingClassId());
        //更新人数
        teachingClassDao.updateTeachingClassAmountOfStudent(updateAmountOfStudentPoReq);
    }

    /**
     * 将学生从教学班级中移除
     *
     * @param studentInTeacherClassBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp removeStudentFromTeacherClass(StudentInTeacherClassBoReq studentInTeacherClassBoReq) throws SQLException {
        StudentInTeachingClassPoReq studentInTeachingClassPoReq
                = CommonUtil.objToObj(studentInTeacherClassBoReq, StudentInTeachingClassPoReq.class);
        studentInTeachingClassPoReq.setTeachingClassId(studentInTeacherClassBoReq.getTeacherClassId());
            CommonBoResp commonBoResp = null;
            CommonPoResp commonPoResp= new CommonPoResp();
            //删除学生
            int i = teachingClassDao.deleteStudentFromTeachingClass(studentInTeachingClassPoReq);
            CommonUtil.printToPoResult(commonPoResp,i);
            commonBoResp = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
            //更新学生人数
            updateStudentAmountOfTeachingClass(studentInTeachingClassPoReq);

        return commonBoResp;
    }



    /**
     * 根据教学班级编号,获取教学班级信息
     *
     * @param teachingClassIdBoReq
     * @return
     */
    @Override
    public TeacherClassUpdateBoResp getUpdateTeacherClassByClassId(TeachingClassIdBoReq teachingClassIdBoReq) throws SQLException {
        TeachingClassPoResp teachingClassPoResp =
                teachingClassDao.getTeachingClassByTeachingClassId(teachingClassIdBoReq.getTeachingClassId());
        //将po对象转成bo对象
        TeacherClassUpdateBoResp teacherClassUpdateBoResp =
                CommonUtil.objToObj(teachingClassPoResp, TeacherClassUpdateBoResp.class);
        CourseIdPoReq courseIdPoReq = new CourseIdPoReq();

        courseIdPoReq.setCourseId(teachingClassPoResp.getCourseID());
        CourseDetailPoResp detail = courseDao.getDetailByCourseId(courseIdPoReq);
        //TODO 查询该课程是否已经存在数据,如果存在数据,将返回对象的状态设置没不可修改课程

        teacherClassUpdateBoResp.setCourseStateBoResp(CommonUtil.objToObj(detail, CourseStateBoResp.class));

        return teacherClassUpdateBoResp;
    }

    /**
     * 教学班级信息展示
     * @param teachingClassIdBoReq
     * @return
     */
    @Override
    public TeacherClassBoResp getTeacherClassByClassId(TeachingClassIdBoReq teachingClassIdBoReq) throws SQLException {
        TeachingClassPoResp teachingClassByTeachingClassId = teachingClassDao.getTeachingClassByTeachingClassId(teachingClassIdBoReq.getTeachingClassId());
        TeacherClassBoResp teacherClassBoResp = CommonUtil.objToObj(teachingClassByTeachingClassId, TeacherClassBoResp.class);
        //通过老师编号查询老师名
        teacherClassBoResp.setTeacherName(teacherDao.getTeacherNameByTeacherId(teachingClassByTeachingClassId.getTeacherID()));
        return teacherClassBoResp;
    }

    /**
     * 修改教学班级
     *
     * @param updateTeacherClassBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp updateTeacherClass(UpdateTeacherClassBoReq updateTeacherClassBoReq) throws SQLException {

        TeachingClassPoReq teachingClassPoReq = CommonUtil.objToObj(updateTeacherClassBoReq , TeachingClassPoReq.class);
        //判断该教学班级下面是否已经产生了学习数据
        //TODO 需要判断
        CommonPoResp commonPoResp=new CommonPoResp();
        int i = teachingClassDao.updateTeachingClass(teachingClassPoReq);
        CommonUtil.printToPoResult(commonPoResp,i);
        return CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
    }
}
